package com.itheima.service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/7/20 15:13
 * @Description:
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements  MemberService {

	@Autowired
	MemberDao memberDao;

	@Override
	public Member findByTelephone(String telephone) {
		return memberDao.findByTelephone(telephone);
	}

	@Override
	public void add(Member member) {
		// 判断用户是否带有密码，如果带有密码，需要加密
		if(!StringUtils.isEmpty(member.getPassword())){
			String md5 = MD5Utils.md5(member.getPassword());
			member.setPassword(md5);
		}
		memberDao.add(member);
	}

	/**
	 *  @描述：根据月份查询会员数量
	 */
	public List<Integer> findCountByMonth(List<String> months) {
		List<Integer> counts = new ArrayList<>();
		for (String month : months) {
			// 2019-07
		//	计算最后一天的日期
		//	2019-07-31
			String end = month+"-31";
			Integer countByMonth = memberDao.findCountByMonth(end);
			counts.add(countByMonth);
		}
		return counts;
	}


	//
	@Override
	public List<Map<String, Object>> findMemberCount() {
		return memberDao.findMemberCount();
	}

	public List<Map> getSexProportion() {
		List<Map> sexMaps = new ArrayList<>();
		List<Map> maps = memberDao.getSexProprotion();
		maps.forEach(map -> {
			Map sexMap = new HashMap();
			map.forEach((key, value) -> {
				if (key.equals("name")) {
					if (value.equals("0")) {
						sexMap.put("name", "男");
					} else if (value.equals("1")) {
						sexMap.put("name", "女");
					} else {
						sexMap.put("name", "未知");
					}
				} else {
					sexMap.put(key, value);
				}
			});
			sexMaps.add(sexMap);
		});
		return sexMaps;
	}


	public Set<String> getSexNames() {
		Set<String> sexs = memberDao.getSexNames();
		Set<String> sexNames = new HashSet<>();
		sexs.forEach((sex) -> {
					if (sex.equals("0")) {
						sexNames.add("男");
					} else if (sex.equals("1")) {
						sexNames.add("女");
					} else {
						sexNames.add("未知");
					}
				}
		);
		return sexNames;
	}

	/**
	 * 会员数量报表指定时间段展示
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, List> findCountBy2Month(String startDate, String endDate) throws Exception{
		HashMap<String, List> map = new HashMap<>();
		//日期格式转换
		startDate = DateUtils.parseDate2String(DateUtils.str2Date(startDate), "yyyy-MM");
		endDate = DateUtils.parseDate2String(DateUtils.str2Date(endDate), "yyyy-MM");
		//获取月份
		List<String> monthBetween = DateUtils.getMonthBetween(startDate, endDate, "yyyy-MM");
		//获取每个月的会员是
		ArrayList<Integer> memberCount = new ArrayList<>();
		for (String i : monthBetween) {
			String first = i + "-01";
			String last = i + "-31";
			Integer member = memberDao.findMemberCountByDateBetween(first, last);
			memberCount.add(member);
		}
		map.put("months", monthBetween);
		map.put("memberCount", memberCount);
		return map;
	}
}
